package com.inspur.edp.common.expr.visitor.construct;


import com.inspur.edp.common.expr.spi.ConstructVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;
import com.inspur.edp.common.expr.visitor.type.JavaTypeReferConverter;
import com.inspur.edp.common.type.Type;
import com.inspur.edp.common.type.TypeRefer;


import java.util.List;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/8/21 16:56
 */
public class JavaArrayConstructVisitor implements ConstructVisitor {

    public static final String MODE = "array";

    @Override
    public String getType() {
        return MODE;
    }

    @Override
    public VisitResult visit(TypeRefer typeRefer, Type type, String parameters) {
        List<TypeRefer> genericTypes = typeRefer.getGenericTypes();
        TypeRefer genericTypeRefer = null;
        if (genericTypes == null || genericTypes.size() == 0) {
            genericTypeRefer = new TypeRefer("Object");
        } else {
            genericTypeRefer = typeRefer.getGenericTypes().get(0);
        }
        String className = JavaTypeReferConverter.getJavaClassName(genericTypeRefer);
        String script = String.format("new %s[%s]", className, parameters);
        VisitResult result = new VisitResult(script);
        return result;
    }
}
